package vlms.servlet.customer;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.Movie;
import vlms.service.VLMSServiceProxy;

/**
 * Servlet implementation class SearchMoviesServlet
 */
@WebServlet("/SearchMoviesServlet")
public class SearchMovies extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VLMSServiceProxy proxy = new VLMSServiceProxy(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchMovies() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		proxy.setEndpoint("http://localhost:8080/VLMS/services/VLMSService");
		
		HttpSession session = request.getSession(true);
		Movie[] movieList=(Movie[]) session.getAttribute("MovieList");
		
		String searchText = request.getParameter("search_text");
		System.out.println("search" + searchText);
		 String type=null;
		 String searchType[]=request.getParameterValues("search");
		 
		 Movie movie=new Movie();
		 
		 if(searchType!=null)	 
		 {
			 
			 if(searchType[0].equals("movieName"))
			 {
				 type="movieName";
				 
				 movie.setMoviename(searchText);
			 }
			 else if(searchType[0].equals("movieBanner"))
			 {
				 type="movieBanner";
				 movie.setMoviebanner(searchText);
			 }
			 else
			 {
				 type="movieCategory";
				 movie.setCategory(searchText);
			 }
			 
			 System.out.println("type : " +type);
		 }
		 
		 try
		 {
			 movieList=proxy.retrieveMovies(movie);
			 request.setAttribute("MovieList", movieList);
			 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/customer/home.jsp");
			 dispatcher.forward(request, response);
		 }
		 catch(RemoteException re)
		 {
			 re.getMessage();
			 re.printStackTrace();
		 }
		
	}

}
